package com.dragee.modules.work.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragee.common.PageUtils;
import com.dragee.common.R;
import com.dragee.constants.CommonConstants;
import com.dragee.modules.work.dao.ShopClerkDao;
import com.dragee.modules.work.entity.ShopClerkEntity;
import com.dragee.modules.work.entity.ShopEntity;
import com.dragee.modules.work.service.ShopClerkService;
import com.dragee.modules.work.service.ShopService;
import com.dragee.utils.LoginUserUtil;
import com.dragee.utils.StringUtils;
import com.dragee.utils.WrapperUtil;
import com.dragee.utils.excel.CommonListener;
import com.dragee.utils.excel.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("shopClerkService")
public class ShopClerkServiceImpl extends ServiceImpl<ShopClerkDao, ShopClerkEntity> implements ShopClerkService {


    @Resource
    private ShopClerkDao shopClerkDao;

    @Autowired
    private ShopService shopService;

    private String business = "shopClerk";
    /**
    * 列表
    */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        int limit = params.get(CommonConstants.LIMIT) == null
                ?CommonConstants.LIMIT_SIZE:Integer.parseInt(params.get(CommonConstants.LIMIT).toString());
        int start = params.get(CommonConstants.START) == null
                ?CommonConstants.START_SIZE:Integer.parseInt(params.get(CommonConstants.START).toString());
        QueryWrapper<ShopClerkEntity> wrapper =
                WrapperUtil.getWrapper(business, params, ShopClerkEntity.class);
        ShopEntity shop = shopService.getShopByUserId(LoginUserUtil.getLoginUserId().toString());
        wrapper.eq("shopId", shop.getId());
        IPage<ShopClerkEntity> page = new Page<>(start, limit);
        page = baseMapper.selectPage(page, wrapper.select());
        return new PageUtils(page);
    }


    @Override
    public R importData(MultipartFile file) throws IOException {
        //填充对应词典 sex: (1:男, 2:女)
        Map<String, Map<String,Object>> map = new HashMap<>();
        //解析器
        CommonListener<ShopClerkEntity> commonListener
                = new CommonListener<>(map, ShopClerkEntity.class,this::excelBatch);
        EasyExcel.read(file.getInputStream())
                .head(ShopClerkEntity.class)
                .registerReadListener(commonListener)
                .sheet()
                .doRead();
        List<String> msgList = commonListener.getMsgList();
        if(msgList.size()>0){
            return R.error(StringUtils.join(msgList,","));
        }
        return R.ok();
    }

    @Override
    public void exportData(Map<String, Object> params, HttpServletResponse response) throws Exception {
        QueryWrapper<ShopClerkEntity> wrapper =
                WrapperUtil.getWrapper(business, params, ShopClerkEntity.class);
        List<ShopClerkEntity> datas = list(wrapper);
        ExcelUtils.writeExcel(response,
                datas,
                "店员" + System.currentTimeMillis(),
                "数据", ShopClerkEntity.class);
    }

    /** 导入执行入库方法
     * @param datas
     * @return
     */
    private boolean excelBatch (List<ShopClerkEntity> datas) {
        for (ShopClerkEntity data : datas) {
            saveOrUpdate(data);
        }
        return true;
    }

}